package com.rgdcx.bus.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rgdcx.bus.mapper.CarMapper;
import com.rgdcx.bus.mapper.RentMapper;
import com.rgdcx.bus.pojo.Customer;
import com.rgdcx.bus.pojo.Rent;
import com.rgdcx.bus.service.IRentService;
import com.rgdcx.bus.vo.CarVo;
import com.rgdcx.bus.vo.RentVo;
import com.rgdcx.sys.constant.SysConstant;
import com.rgdcx.sys.utils.DataGridView;
import com.rgdcx.sys.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
public class RentServiceImpl implements IRentService {
    @Autowired
    private RentMapper rentMapper;

    @Autowired
    private CarMapper carMapper;

    @Override
    public void addRent(RentVo rentVo) {
        this.rentMapper.insertSelective(rentVo);
        // 更改车辆的出租信息
        CarVo carVo = new CarVo();
        carVo.setCarnumber(rentVo.getCarnumber());
        // 设置车辆的状态为已出租
        carVo.setIsrenting(SysConstant.RENT_BANK_TRUE);
        carMapper.updateByPrimaryKeySelective(carVo);
    }

    /**
     * 分页查询出租单
     * @param rentVo
     * @return
     */
    @Override
    public DataGridView queryAllRent(RentVo rentVo) {
        Page<Object> page = PageHelper.startPage(rentVo.getPage(), rentVo.getLimit());
        // 做全查询
        List<Rent> data = rentMapper.queryAllRent(rentVo);
        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public void updateRent(RentVo rentVo) {
        rentMapper.updateByPrimaryKeySelective(rentVo);
    }

    @Override
    public void deleteRent(RentVo rentVo) {
        CarVo carVo = new CarVo();
        carVo.setCarnumber(rentVo.getCarnumber());
        // 设置车辆状态为未出租
        carVo.setIsrenting(SysConstant.RENT_CAR_FALSE);
        // 删除出租单
        rentMapper.deleteByPrimaryKey(rentVo.getRentid());
        // 更新车辆状态
        carMapper.updateByPrimaryKeySelective(carVo);

    }

    @Override
    public Rent queryRentByRentId(String rentid) {
        return rentMapper.queryRentById(rentid);
    }


}
